package cn.lili.controller.feign;

import cn.hutool.core.collection.CollectionUtil;
import cn.lili.common.utils.StringUtils;
import cn.lili.modules.goods.client.GoodsSkuClient;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
import cn.lili.modules.goods.entity.dto.GoodsSkuDTO;
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
import cn.lili.modules.goods.entity.enums.GoodsMarketEnum;
import cn.lili.modules.goods.integration.GoodsIntegrationService;
import cn.lili.modules.goods.service.GoodsSkuService;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: ftyy
 * @date: 2022-01-12 10:57
 * @description: GoodsSku feign Client
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class GoodsSkuFeignController implements GoodsSkuClient {

    private final GoodsSkuService goodsSkuService;

    private final GoodsIntegrationService goodsIntegrationService;

    @Override
    public GoodsSku getGoodsSkuByIdFromCache(String id) {
        return goodsSkuService.getGoodsSkuByIdFromCache(id);
    }

    /**
     * 批量从redis中获取商品SKU信息
     *
     * @param ids SkuId集合
     * @return 商品SKU信息集合
     */
    @Override
    public List<GoodsSku> getGoodsSkuByIdFromCache(List<String> ids) {
        return goodsSkuService.getGoodsSkuByIdFromCache(ids);
    }

    @Override
    public Integer getStock(String skuId) {
        return goodsSkuService.getStock(skuId);
    }


    @Override
    public void updateGoodsSkuCommentNum(String skuId) {
        goodsIntegrationService.updateGoodsSkuCommentNum(skuId);
    }

    @Override
    public void updateBatchById(List<GoodsSku> goodsSkus) {
        goodsSkuService.updateBatchById(goodsSkus);
    }

    @Override
    public IPage<GoodsSku> getGoodsSkuByPage(GoodsSearchParams searchParams) {
        return goodsSkuService.page(PageUtil.initPage(searchParams), searchParams.queryWrapper());
    }

    @Override
    public Long getGoodsSkuCount(GoodsSearchParams searchParams) {
        return goodsSkuService.count(searchParams.queryWrapper());
    }

    @Override
    public Page<GoodsSkuDTO> getGoodsSkuDTOByPage(GoodsSearchParams searchParams) {
        return goodsSkuService.getGoodsSkuDTOByPage(PageUtil.initPage(searchParams), searchParams.queryWrapperGoodsSkuByCustomSql());
    }

    @Override
    public List<GoodsSku> getGoodsSkuByGoodsIdList(List<String> goodsIdList) {
        if (CollectionUtil.isEmpty(goodsIdList)) {
            log.error("goodsIdList === 为空");
            return new ArrayList<>();
        }
        LambdaQueryWrapper<GoodsSku> goodsSkuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        goodsSkuLambdaQueryWrapper.in(GoodsSku::getGoodsId, goodsIdList);
        goodsSkuLambdaQueryWrapper.eq(GoodsSku::getDeleteFlag, Boolean.FALSE);
        return goodsSkuService.list(goodsSkuLambdaQueryWrapper);
    }

    @Override
    public GoodsSku getGoodsSkuById(String skuId) {
        return goodsSkuService.getGoodsSkuById(skuId);
    }

    @Override
    public void updateGoodsSkuCache(List<String> goodsIds) {
        goodsSkuService.updateGoodsSkuCache(goodsIds);
    }

    @Override
    public List<GoodsSku> getGoodsSkuByStoreIds(List<String> storeIds) {
        return goodsSkuService.getGoodsSkuByStoreIds(storeIds);
    }
}
